package com.example.demo.repository;

import com.example.demo.model.Message;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
    List<Message> findBySenderIdAndReceiverIdOrReceiverIdAndSenderId(
            Long senderId1, Long receiverId1, Long senderId2, Long receiverId2);

    @Modifying
    @Query("UPDATE Message m SET m.isRead = true WHERE m.sender.id = ?1 AND m.receiver.id = ?2")
    void markMessagesAsRead(Long senderId, Long receiverId);

    long countByReceiverIdAndIsReadFalse(Long receiverId);
}